import GameData_blxf from "../GameData/GameData";
import { VendorData_blxf } from "../GameData/VendorData";
import { CsvManager_blxf } from "../Manager/CsvManager";
import { EnumManager_blxf } from "../Manager/EnumManager";
import PageManager_blxf from "../Manager/PageManager";
import { ConfigModule_blxf } from "../Module/ConfigModule";
import { LoadModule_blxf } from "../Module/LoadModule";
import { NumModule_blxf } from "../Module/NumModule";
import PublicModule_blxf from "../Module/PublicModule";
import { UtilsModule_blxf } from "../Module/UtilsModule";
import VendorInfoItem_blxf from "./vendorInfoItem";



const {ccclass, property} = cc._decorator;

/** 小摊贩关卡面板信息 */

@ccclass
export class VendorInfoPanel_blxf extends cc.Component {

    @property({ type: cc.Node, tooltip: '金币图标' })
    goldIcon: cc.Node = null;

    @property({ type: cc.Node, tooltip: '元宝图标' })
    diamondIcon: cc.Node = null;

    @property({ type: cc.Node, tooltip: '大关卡数' })
    bigPassNumNode: cc.Node = null;

    @property({ type: cc.Label, tooltip: '关卡数' })
    passNumText: cc.Label = null;

    @property({ type: cc.Node, tooltip: '下一大关数' })
    nextPassNumText: cc.Node = null;

    @property({ type: cc.Sprite, tooltip: '关卡进度' })
    passProSpr: cc.Sprite = null;

    @property({ type: cc.Sprite, tooltip: '关卡需求商品' })
    shopImgSpr: cc.Sprite = null;

    @property({ type: cc.Label, tooltip: '金币数' })
    goldNumText: cc.Label = null;

    @property({ type: cc.Label, tooltip: '元宝数' })
    diamondNumText: cc.Label = null;

    @property({ type: cc.Node, tooltip: '动画节点' })
    animationNode: cc.Node = null;

    @property({ type: cc.Node, tooltip: '关卡商品列表' })
    passShopBox: cc.Node = null;

    @property({ type: cc.Node, tooltip: '进度数值盒子' })
    progressBox: cc.Node = null;

    @property({ type: cc.Tween, tooltip: '关卡进度动画' })
    _passTween: cc.Tween = null;

    @property({ type: cc.Tween, tooltip: '火力动画' })
    _animationTween: cc.Tween = null;

    _progressNum: number = 0; 

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        cc.game.on('onUpdateTopNum_blxf', this.updateUI_blxf, this);
        // cc.game.on('onUpdateTopShopImg_blxf', this.updateShopImg_blxf, this);
        cc.game.on('onUpdateTopPassInfo_blxf', this.updatePassInfo_blxf, this);
        cc.game.on('initIconPosition_blxf', this.initIconPosition_blxf, this);

        // NumModule_blxf.instance.updateNumByType( 1, 10000 );
        // NumModule_blxf.instance.updateNumByType( 2, 2000 );
    }

    start () {
        this.updateUI_blxf( true );
        this.updatePassInfo_blxf( true );

        this.initSparkAnimation_blxf();

        this.initIconPosition_blxf();

        this.node.opacity = 0;
    }

    onDestroy () { cc.game.removeAll(this) }

    // update (dt) {}

    initIconPosition_blxf () {
        PublicModule_blxf.goldIconPos = UtilsModule_blxf.instance.getCanvasPos_blxf( this.goldIcon );
        PublicModule_blxf.diamondIconPos = UtilsModule_blxf.instance.getCanvasPos_blxf( this.diamondIcon );
        UtilsModule_blxf.instance.Log_blxf([ 'gold, diamond =>', PublicModule_blxf.goldIconPos, PublicModule_blxf.diamondIconPos ]);
    }

    /** 加载火力动画 */
    initSparkAnimation_blxf () {
        // PageManager_blxf.instance.loadSpineProgressSpark_blxf( this.animationNode );
    }

    updateUI_blxf ( isLoad: boolean ) {
        this.updateNum_blxf(1);
        this.updateNum_blxf(2);
    }

    /**
     * 更新货币UI
     * @param iconType 货币类型 
     */
    updateNum_blxf ( iconType: number ) {
        let { NUM_GOLD, NUM_DIAMOND } = EnumManager_blxf.gameNumType;
        switch ( iconType ) {
            case NUM_GOLD    : this.updateGold_blxf(); break;
            case NUM_DIAMOND : this.updateDiamond_blxf(); break;
        }
    }

    /** 更新关卡信息 */
    updatePassInfo_blxf ( isLoad: boolean ) {
        isLoad && this.updatePassNum_blxf();
        this.updatePassProgress_blxf( isLoad );
    }

    /**
     * 更新金币数
     * @param goldNum 金币数
     */
    updateGold_blxf ( goldNum: number = -1 ) {
        if ( goldNum == -1 ) {
            goldNum = NumModule_blxf.instance.getNumByType( 1 );
        }
        UtilsModule_blxf.instance.setText_blxf( this.goldNumText.node, `${goldNum}` );
    }

    /**
     * 更新元宝数
     * @param diamondNum 元宝数
     */
    updateDiamond_blxf ( diamondNum: number = -1 ) {
        if ( diamondNum == -1 ) {
            diamondNum = NumModule_blxf.instance.getNumByType( 2 );
        }
        UtilsModule_blxf.instance.setText_blxf( this.diamondNumText.node, `${diamondNum}` );
    }

    /** 更新关卡数 */
    updatePassNum_blxf () {
        let _passNum: number = GameData_blxf.instance.getPassNum_blxf();
        let _bigPassNum: number = GameData_blxf.instance.getBigPassNum_blxf();
        _bigPassNum = Math.ceil( _passNum / ConfigModule_blxf.basePassNum );

        UtilsModule_blxf.setText_blxf( this.passNumText.node, `${_passNum}` );
        UtilsModule_blxf.setText_blxf( this.bigPassNumNode, `第${_bigPassNum}关` );
        UtilsModule_blxf.setText_blxf( this.nextPassNumText, `${_bigPassNum + 1}` );
    }

    /** 更新关卡进度 */
    updatePassProgress_blxf ( isLoad: boolean ) {
        let _passShopNum: number = GameData_blxf.instance.getPassShopNum_blxf();
        let _passShopInfo = VendorData_blxf.instance.getTargetPassShopInfo_blxf();
        // 菜品已解锁完
        if ( _passShopInfo == -1 ) {
            return this.shopImgSpr.node.active = false;
        }

        this.updateShopImg_blxf( _passShopInfo, isLoad );

        let { NeedShopNum } = _passShopInfo;
        let _isInit: boolean = !isLoad && _passShopNum == 0;
        let _passProgress: number = /*_isInit ? 1 :*/ _passShopNum / NeedShopNum;
        let _nowProgress: number = _passProgress;

        _passProgress += this._progressNum;
        _passProgress /= 3;

        // 大关过关
        if ( _passProgress == 0 && _isInit ) _passProgress = 1;

        // cc.log('_passProgress >>', _passProgress, this._progressNum);

        this._passTween && this._passTween.stop();
        this._animationTween && this._animationTween.stop();
        this._passTween = null, this._animationTween = null;

        let _t: number = (isLoad || _passProgress == 0) ? 0 : 0.3;
        this._passTween = cc.tween( this.passProSpr ).to(_t, { fillRange: _passProgress })
        .call(()=>{
            this.updateProgressNum_blxf( _nowProgress );
        //     if ( !_isInit ) return;
        //     let _delayTime: number = 0.2;
        //     cc.tween( this.passProSpr ).delay(_delayTime).to(0, { fillRange: 0 }).call(()=>{
        //         this.updateShopImg_blxf( _passShopInfo );
        //     }).start();
        })
        .start();

        this.animationNode.opacity = !isLoad && _passProgress > 0 ? 255 : 0;
        let _maxX = this.passProSpr.node.width;
        let _x: number = _passProgress * _maxX;
        if ( !isLoad ) {
            
            this._animationTween = cc.tween( this.animationNode )
            .to(_t, { x: _x })
            .delay(0.1)
            .to(0.2, { opacity: 0 })
            .call(()=>{
                if ( _isInit ) {
                    this.animationNode.x = 0;
                    // this.animationNode.opacity = 0;
                }
                this.animationNode.opacity = 0;
            })
            .start();
        } else {
            this.animationNode.x = _x;
        }
    }

    /** 更新百分比进度值 */
    updateProgressNum_blxf ( num: number ) {
        let _percentStr: string = NumModule_blxf.instance.changePercent_blxf( num );

        this.progressBox.children.forEach( (item, index) =>{
            let _isShow: boolean = index == this._progressNum;
            item.active = _isShow;

            if ( !_isShow ) return;
            
            UtilsModule_blxf.setText_blxf( item, _percentStr );
        })
    }

    /** 更新商品图片 */
    updateShopImg_blxf ( _shopInfo, isLoad ) {
        // let { AtlasName, ImgName } = _shopInfo;

        // let imgBox: cc.Node = this.shopImgSpr.node.parent;
        // let _options = {
        //     path: AtlasName, imgName: ImgName, 
        //     imgSpr: this.shopImgSpr, imgBox, isSpecial: true
        // }
        // LoadModule_blxf.instance.LoadImg_blxf( _options );

        this._progressNum = 0;

        this.onUpdatePassShopData_blxf( 0, isLoad );
        this.onUpdatePassShopData_blxf( 1, isLoad );
        this.onUpdatePassShopData_blxf( 2, isLoad );
    }


    onUpdatePassShopData_blxf ( index: number, isLoad ) {
        let _shopIdList: number[] = GameData_blxf.instance.getShopIdList_blxf();
        // let _nowShopId: number = _shopIdList[ _shopIdList.length - 1 ];
        let _passNum = GameData_blxf.instance.getPassNum_blxf();
        let _smallPassNum: number = ( _passNum - 1 ) % 3;
        let _passShopId: number = _passNum - ( _passNum - 1 ) % ConfigModule_blxf.basePassNum + index;
        let { CSV_SHOP } = EnumManager_blxf.instance.getGameCsvType();
        let _cfgShopData: any[] = CsvManager_blxf.instance.getCsvDataByName_blxf( CSV_SHOP );
        let _shopIndex: number = _cfgShopData.findIndex( item => item.ShopId == _passShopId );
        let _nextShopData: any = _cfgShopData[ _shopIndex ];
        // if ( !_nextShopData ) return;
        if ( !_nextShopData ) _nextShopData = UtilsModule_blxf.instance.onCommonShopData_blxf( index );
 
        let { AtlasName, ImgName } = _nextShopData;
        let isPass: boolean = _passNum > _passShopId || (_smallPassNum > index);
        // cc.log(isLoad, '_passShopId >>', _passNum, _passShopId);
        let _data: any = { imgPath: AtlasName, imgName: ImgName, isPass };

        let _itemNode: cc.Node = this.passShopBox.children[ index ];
        isLoad && _itemNode && _itemNode.getComponent( VendorInfoItem_blxf).initData( _data );

        if ( isPass ) this._progressNum += 1;
    }

}
